/**
 * Created by haocongshun on 2016/9/14.
 */
var contextPath = "/";
$(function () {
    contextPath += window.location.pathname.split("/")[1];
    init();
    bindreturn();
});
function bindreturn() {
    $("#feedback").click(function () {
        var url=contextPath+"/message/reLeaveMessage.wm"
        var data={};
        data.from=location.search.split("&")[0].substring(1);
        data.to=location.search.split("=")[1];
        if($.trim($("#rediv textarea").val())==""||$.trim($("#rediv textarea").val())==null){
            alert("回复内容不能为空！");
            return;
        }
        data.remsg=$.trim($("#rediv textarea").val());
        $.post(url,data,function (result) {
            if(result.msg){
                init();
            }else {
                alert("回复失败！");
            }
        },"json");
    });
    $("#cancel").click(function () {
        history.go(-1);
    });
}
function init() {
    var url = contextPath+"/message/leaveMessage.wm"
    var data={};
    data.flag=location.search.split("&")[0].substring(1);
    data.to=location.search.split("=")[1];
    var from = data.flag=="tea"?"teacher":"student";
    $.post(url,data,function (result) {
        var newHtml = "";
        for(var i = 0;i < result.leaveMessage.length;i++){
            var date = result.leaveMessage[i].leaveMessage_date;
            date = date.substring(0,date.length-2);
            if(from==result.leaveMessage[i].leaveMessage_from){
                newHtml+="<dl class='vme m1'>" +
                    " <dt><a>我</a>" +
                    "<span>"+date+"</span>" +
                    "</dt>" +
                    " <dd>"+result.leaveMessage[i].leaveMessage_content+"</dd>" +
                    " </dl>";
            }else {
                var satues = (from=="teacher"?(result.leaveMessage[i].aStudent.student_name):(result.leaveMessage[i].aTeacher.teacher_name));
                newHtml+="<dl class='vhe m1'>"+
                    " <dt><a>"+satues+ "</a>" +
                    "<span>"+date+"</span>" +
                    "</dt>" +
                    " <dd>"+result.leaveMessage[i].leaveMessage_content+"</dd>" +
                    " </dl>";
            }
        }
        $("#talk").html(newHtml);
        $("#rediv h4 span").html(from=="teacher"?(result.leaveMessage[0].aStudent.student_name+"同学"):(result.leaveMessage[0].aTeacher.teacher_name+"老师"));
    },"json");
}